﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Collections;
using Microsoft.Win32;

namespace eBirdAttend
{
    public partial class ReportFwdConfig : UserControl
    {
        SqlConnection _sqlcon;
        SqlCommand _sqlcmd;
        DataSet ds;
        DataSet ds1;
        DataTable dt;
        DataTable dt1;
        SqlDataAdapter _sqladt;
        SqlDataReader _sqldr;

        string[] strEMailId = new string[5]; 

        #region Properties

        string _strCon;

        public string StrCon
        {
            get { return _strCon; }
            set { _strCon = value; }
        }
        
        #endregion Properties

        public ReportFwdConfig()
        {
            InitializeComponent();
            FillConnectionStr();
        }      
        
        private void ReportFwdConfig_Load(object sender, EventArgs e)
        {
            cmbRepType.SelectedIndex = 0;
            cmbFwdType.SelectedIndex = 0;
            cmbWeek.SelectedIndex = 0;
            cmbReceipents.SelectedIndex = 0;
            rdbDaily.Checked = true;
            
        }

        private void FillConnectionStr()
        {
            //Connection string stored in REgistry
            RegistryKey regKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\eBird\\eOffice1.0");
            if (regKey != null)
            {
                _strCon = regKey.GetValue("ConctString").ToString();
            }
        }        


        public void LogWrite(string Log)
        {
            try
            {
                string strFileName = "LogFile.txt";
                StreamWriter sWriter = new StreamWriter(strFileName, true);
                DateTime dateTime = DateTime.Now;
                sWriter.WriteLine(dateTime + "\r\t" + Log + "\r\n");
                sWriter.Close();
                sWriter.Dispose();
            }
            catch (Exception ex)
            {
                //NOTHING TO DO....
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ControlsRequired())
            {                
                    SaveToDatabase();
                
            }
            else
            {
                MessageBox.Show("Please insert all fields");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private bool ControlsRequired()
        {
            if (txtSchName.Text == "")
                return false;
            else if (txtSubject.Text == "")
                return false;
            else if (txtFrom.Text == "")
                return false;
            else if (txtTo.Text == "")
                return false;
            else if (txtServer.Text == "")
                return false;
            else if (txtPort.Text == "")
                return false;
            else if (txtUser.Text == "")
                return false;
            else if (txtPwd.Text == "")
                return false;
            else if (txtRptTemplate.Text == "")
                return false;
            else
                return true;
        }
     
        private void SaveToDatabase()
        {
            try
            {
                string _strSchType="";
                string _strWeekDay = "";
                int _month=0;
                if (rdbDaily.Checked)
                {
                    _strSchType = "daily";
                }
                else if (rdbWeekly.Checked)
                {
                    _strSchType = "weekly";
                    _strWeekDay = cmbWeek.SelectedValue.ToString();
                }
                else if (rdbMonthly.Checked)
                {
                    _strSchType = "monthly";
                    _month = Convert.ToInt32(txtMonth.Text);
                }

                _sqlcon = new SqlConnection(_strCon);
                _sqlcmd = new SqlCommand();
                _sqlcmd.Connection = _sqlcon;
                _sqlcon.Open();
                _sqlcmd.CommandText = "insert into ReportScheduler(Name,ReportType, " 
                    + "ForwardType,ScheduleType,WeekDay,DateofMonth,Destination,SourceID,"
                    + "SubjectLine,UserName,Password,Server,Port,ReportTemplate)"
                    + " values ( '" + txtSchName.Text + "',' " 
                    + cmbRepType.SelectedText + "','" + cmbFwdType.SelectedText
                    + "',' "+ _strSchType +"','" + _strWeekDay +"',"+ _month
                    + ",' " + txtTo.Text + "','" + txtFrom.Text +"','" + txtSubject.Text 
                    + "','" + txtUser.Text + "','" + txtPwd.Text + "','" + txtServer.Text
                    + "','" + txtPort.Text + "','" + txtRptTemplate.Text + "')" ;
                _sqlcmd.ExecuteNonQuery();
                _sqlcon.Close();
                MessageBox.Show("Saved Successfully");
                ClearControls();
                     
            }
            catch (Exception ex)
            {
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {

            OpenFileDialog _openFileDialog = new OpenFileDialog();
            if (_openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string _strPath = _openFileDialog.FileName;
                string[] _strFileName = _strPath.Split('\\');
                int i;
                for (i = 0; i < _strFileName.Length;i++ )
                {
                    if ((i + 1) == _strFileName.Length)
                        txtRptTemplate.Text = _strFileName[i];
                }
            }
        }

        private void ClearControls()
        {
            txtFrom.Text = "";
            txtMonth.Text = "";
            txtPort.Text = "";
            txtPwd.Text = "";
            txtRptTemplate.Text = "";
            txtSchName.Text = "";
            txtServer.Text = "";
            txtSubject.Text="";
            txtTo.Text="";
            txtUser.Text="";
            cmbFwdType.SelectedIndex=0;
            cmbReceipents.SelectedIndex=0;
            cmbRepType.SelectedIndex=0;
            cmbWeek.SelectedIndex=0;
        }

     
        

      
    }

}
